package com.mqstudy.kafka.config;

import com.mqstudy.common.model.Message;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.serializer.JsonSerializer;

import java.util.HashMap;
import java.util.Map;

/**
 * Kafka配置类
 */
@Configuration
public class KafkaConfig {
    
    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;
    
    // 主题名称
    public static final String TEST_TOPIC = "test-topic";
    public static final String ORDER_TOPIC = "order-topic";
    public static final String USER_TOPIC = "user-topic";
    public static final String LOG_TOPIC = "log-topic";
    public static final String STREAM_TOPIC = "stream-topic";
    
    /**
     * 创建主题
     */
    @Bean
    public NewTopic testTopic() {
        return TopicBuilder.name(TEST_TOPIC)
                .partitions(3)
                .replicas(1)
                .build();
    }
    
    @Bean
    public NewTopic orderTopic() {
        return TopicBuilder.name(ORDER_TOPIC)
                .partitions(5)
                .replicas(1)
                .build();
    }
    
    @Bean
    public NewTopic userTopic() {
        return TopicBuilder.name(USER_TOPIC)
                .partitions(2)
                .replicas(1)
                .build();
    }
    
    @Bean
    public NewTopic logTopic() {
        return TopicBuilder.name(LOG_TOPIC)
                .partitions(10)
                .replicas(1)
                .build();
    }
    
    @Bean
    public NewTopic streamTopic() {
        return TopicBuilder.name(STREAM_TOPIC)
                .partitions(8)
                .replicas(1)
                .build();
    }
    
    /**
     * 生产者工厂配置
     */
    @Bean
    public ProducerFactory<String, Message> producerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
        configProps.put(ProducerConfig.ACKS_CONFIG, "all");
        configProps.put(ProducerConfig.RETRIES_CONFIG, 3);
        configProps.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
        configProps.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
        configProps.put(ProducerConfig.LINGER_MS_CONFIG, 1);
        configProps.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");
        configProps.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
        
        return new DefaultKafkaProducerFactory<>(configProps);
    }
    
    /**
     * Kafka模板配置
     */
    @Bean
    public KafkaTemplate<String, Message> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
} 